﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;

namespace Y2_MineLand
{
    public partial class Form1 : Form
    {
        int second = 0, tam = 0, dem = 0;
        static public string time = null;
        MinesBoard mines = new MinesBoard();
        public Form1()
        {
            InitializeComponent();
            this.Text = Application.ProductName + " " + Application.ProductVersion;
            NewGame();
        }
        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NewGame();
        }
        private void NewGame()
        {
            MediaPlayer.Sound_newgame.Play();
            lblTime.Text = "00 : 00";
            dem = second = tam = 0;
            minesBoard1.NewGame();
            RenewForm();
        }
        private void NewGame(int rows,int cols,int mines)
        {
            MediaPlayer.Sound_newgame.Play();
            lblTime.Text = "00 : 00";
            dem = second = tam = 0;
            minesBoard1.NewGame(rows,cols,mines);
            RenewForm();
        }
        private void RenewForm()
        {           
            button1.BackgroundImage = Properties.Resources.face1;
            this.Height = minesBoard1.Height + 100;
            this.Width = minesBoard1.Width + 20;
            UpdateMines();
        }
        void UpdateMines()
        {
            lblMines.Text = String.Format("{0:000}",minesBoard1.MinesCount - minesBoard1.FlagsCount);
            if (minesBoard1.RemainCellsCount == minesBoard1.MinesCount)//Hoan thanh
                button1.BackgroundImage = Properties.Resources.face4;
            if (minesBoard1.IsLost == true)
            {
                timer1.Stop();
            }
        }
        void CheckMenuItem(ToolStripMenuItem menuItem)
        {
            foreach (var item in gameToolStripMenuItem1.DropDownItems)
            {
                ToolStripMenuItem mi = item as ToolStripMenuItem;
                if(mi!=null)
                    mi.Checked= false;
            }
            menuItem.Checked = true;
        }

        private void beginnerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NewGame(9, 9, 10);
            CheckMenuItem(sender as ToolStripMenuItem);            
        }

        private void intermediateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NewGame(16, 16, 30);
            CheckMenuItem(sender as ToolStripMenuItem);            
        }

        private void expertToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NewGame(25, 40, 200);
            CheckMenuItem(sender as ToolStripMenuItem);            
        }

        private void customToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CustomForm frm = new CustomForm(minesBoard1.Rows,minesBoard1.Cols,minesBoard1.MinesCount);

            if (frm.ShowDialog() == DialogResult.OK)
            {
                NewGame(frm.Rows, frm.Cols, frm.Mines);
            }
            CheckMenuItem(sender as ToolStripMenuItem);  
            
        }
        private void minesBoard1_MinesExplode(object sender, EventArgs e)
        {
            MediaPlayer.Sound_bomno.Play();
            button1.BackgroundImage = Properties.Resources.face3;
            timer1.Stop();

        }
        private void minesBoard1_CellClick(object sender, EventArgs e)
        {
            dem++;
            if (dem == 1)
            {
                MediaPlayer.Sound_Click.Play();
                tam = 0;
                timer1.Start();
            }
            UpdateMines();
            if (MinesBoard._IsFinish)
            {
                MediaPlayer.Sound_Wingame.Play();
                timer1.Stop();
                time = lblTime.Text;
                Xep_Hang frm2 = new Xep_Hang();
                frm2.ShowDialog();
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            NewGame();
            
        }
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            tam++;
            if (tam == 10)
            {
                second++; tam = 0;
            }
            lblTime.Text = second + " : " + tam;
        }
        private void cloToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ColorDialog dlg = new ColorDialog();
            dlg.Color = minesBoard1.MaskColor;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                minesBoard1.MaskColor = dlg.Color;
            }
        }
        private void aboutToolStripMenuItem2_Click(object sender, EventArgs e)
        {
            About about = new About();
            about.ShowDialog();
        }

        private void aboutToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Option option = new Option();
            option.ShowDialog();
        }
    }
}
